#include "GameManager.h"

GameManager::GameManager()
{
	gs = GameState::START;
	gd = AIController::GameDifficulty::EASY;
	pc = NULL;
	ai = NULL;
}

void GameManager::StartGame()
{
	if (gs == GameState::START) {
		InitGame();
	}
}

void GameManager::RestartGame()
{
	if (gs == GameState::WIN || gs == GameState::LOSE) {
		delete pc;
		delete ai;
		pc = NULL;
		ai = NULL;
		gs = GameState::START;
	}
}

void GameManager::Update(set<GameObject*>* objects)
{
	Soldier* ps = NULL;
	if ((ps = pc->Update(objects)) != NULL) {
		objects->insert(ps);
	}
	Soldier* as = NULL;
	if ((as = ai->Update(objects)) != NULL) {
		objects->insert(as);
	}

	if (!ai->GetBase()->IsAlive()) {
		pc->ClearTrainQueue();
		gs = GameState::WIN;
	}
	else if (!pc->GetBase()->IsAlive()) {
		pc->ClearTrainQueue();
		gs = GameState::LOSE;
	}
}

PlayerController* GameManager::GetPlayerController()
{
	return pc;
}

AIController* GameManager::GetAIController()
{
	return ai;
}

GameManager::GameState GameManager::GetGameState()
{
	return gs;
}

void GameManager::SetGameDifficulty(AIController::GameDifficulty _gd)
{
	if (gs == GameState::START) {
		gd = _gd;
	}
}

AIController::GameDifficulty GameManager::GetGameDifficulty()
{
	return gd;
}

void GameManager::InitGame()
{
	pc = new PlayerController();
	ai = new AIController(gd);
	pc->SetEnemy(ai);
	ai->SetEnemy(pc);
	gs = GameState::GAME;
}
